var gulp = require('gulp');
var shell = require('gulp-shell');

// [Neo] TODO change here
var project_name = 'pm';
// [Neo] refer to `settings.gradle`
var module_name = 'pm';
// [Neo] static file from local
var static_web_path = '-';
// [Neo] static file to http server
var static_web_name = '-';

var docker_web_name = project_name + '_web_1';
var docker_db_name = project_name + '_db_1';
var docker_cache_name = project_name + '_cache_1';

var docker_project_volume = '/srv/docker/' + project_name;
var docker_web_path = docker_project_volume + '/web';
var docker_db_path = docker_project_volume + '/db';
var docker_cache_path = docker_project_volume + '/cache';

var security_java_file = '/etc/java-8-openjdk/security/java.security';

var nginx_server_conf_path = '/etc/nginx/default.d';
var nginx_reload_cmd = 'sudo systemctl reload nginx';
var nginx_server_conf_path_mac = '/usr/local/etc/nginx/default.d';
var nginx_reload_cmd_mac = 'sudo brew services restart nginx';
var nginx_root = '/srv/http';
var nginx_html_path = '/usr/share/nginx/html';
var nginx_html_path_mac = '/usr/local/var/www';

var db_in_docker = true;
var db_name = module_name;
var db_user = module_name;
// [Neo] TODO check here
var db_root_password = 'root';

gulp.task('default', shell.task([
  'if [ ! -e ' + nginx_root + ' ]; then if [ "Linux" = $(uname) ]; then sudo ln -s ' + nginx_html_path + ' ' + nginx_root + '; else sudo mkdir -p /srv; sudo ln -s ' + nginx_html_path_mac + ' ' + nginx_root + '; fi; else if [ ! -L ' + nginx_root + ' ]; then echo ' + nginx_root + ' check failed!; exit 1; fi; fi',
  'if [ "Darwin" = $(uname) ]; then sudo mkdir -p ' + docker_project_volume + '/{web,db,cache} && sudo chown -R $USER ' + docker_project_volume + '; fi',
  'c=`cat .env` && t=`cat templates/docker-compose.template` && echo "$c\ncat<<EOF\n$t\nEOF" | bash > docker-compose.yml',
  'docker-compose up -d',
]));

gulp.task('stop', shell.task([
  'docker-compose stop',
]));

gulp.task('start', shell.task([
  'docker-compose start',
]));

gulp.task('clean', shell.task([
  'docker-compose down',
  'sudo rm -rf ' + docker_project_volume,
  'sudo rm -rf ' + nginx_server_conf_path + '/' + module_name + '.*',
  'sudo rm -rf ' + nginx_server_conf_path_mac + '/' + module_name + '.*',
]));

gulp.task('bye', ['clean'], shell.task([
  'sudo rm -rf .git src build node_modules templates backend web *.{sql,yml,md,json,js,gradle,conf,https}',
]));

gulp.task('acc-tomcat', shell.task([
  'docker cp ' + docker_web_name + ':' + security_java_file + ' /tmp/',
  'if [ "Linux" = $(uname) ]; then sed -i \"s\/source=file:\\\/dev\\\/random/source=file:\\\/dev\\\/.\\\/urandom/g\" /tmp/java.security; else sed -i "" \"s\/source=file:\\\/dev\\\/random/source=file:\\\/dev\\\/.\\\/urandom/g\" /tmp/java.security; fi',
  'docker cp /tmp/java.security ' + docker_web_name + ':' + security_java_file,
  'rm /tmp/java.security',
]));

gulp.task('acc-deploy', shell.task([
  'if [ -e deploy-local-v0.1.zip ]; then 7za x -y deploy-local-v0.1.zip && cd deploy-local; 7za x -y npm-root.zip -o..; 7za x -y npm-web.zip -o../web; 7za x -y nginx-https.zip && sh apply-nginx.sh; else echo \\"deploy-local\\" file check failed!; fi;',
  'rm -rf deploy-local',
]));

gulp.task('lock-mysql-root', shell.task([
  'echo "DELETE FROM user WHERE user = \'root\' AND host = \'%\';" > /tmp/lock.sql',
  'echo "FLUSH PRIVILEGES;" >> /tmp/lock.sql',
  'docker exec -i ' + docker_db_name + ' mysql -uroot -p' + db_root_password + ' mysql < /tmp/lock.sql',
  'rm /tmp/lock.sql',
]));

gulp.task('restore', ['acc-tomcat'], shell.task([
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysql -uroot -p' + db_root_password + ' --default-character-set=utf8mb4 < db.sql',
]));

gulp.task('restore-rows', shell.task([
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysql -uroot -p' + db_root_password + ' --default-character-set=utf8mb4 < db.rows.sql',
]));

gulp.task('dump', shell.task([
  'echo "--" > db.sql',
  'echo "-- DO NOT EDIT THIS FILE!" >> db.sql',
  'echo "-- ----------------------" >> db.sql',
  'echo "--" >> db.sql',
  'echo "" >> db.sql',
  'echo "DROP DATABASE IF EXISTS ' + db_name + ';" >> db.sql',
  'echo "CREATE DATABASE ' + db_name + ';" >> db.sql',
  'echo "GRANT ALL PRIVILEGES ON ' + db_name + '.* TO ' + db_user + '@\'%\' IDENTIFIED BY \'' + db_user + '.docker\';" >> db.sql',
  'echo "USE ' + db_name + ';" >> db.sql',
  'echo "" >> db.sql',
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysqldump -uroot -p' + db_root_password + ' ' + db_name + ' -d >> db.sql',
]));

gulp.task('dump-rows', shell.task([
  'echo "--" > db.rows.sql',
  'echo "-- DO NOT EDIT THIS FILE!" >> db.rows.sql',
  'echo "-- ----------------------" >> db.rows.sql',
  'echo "--" >> db.rows.sql',
  'echo "" >> db.rows.sql',
  'echo "DROP DATABASE IF EXISTS ' + db_name + ';" >> db.rows.sql',
  'echo "CREATE DATABASE ' + db_name + ';" >> db.rows.sql',
  'echo "GRANT ALL PRIVILEGES ON ' + db_name + '.* TO ' + db_user + '@\'%\' IDENTIFIED BY \'' + db_user + '.docker\';" >> db.rows.sql',
  'echo "USE ' + db_name + ';" >> db.rows.sql',
  'echo "" >> db.rows.sql',
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysqldump -uroot -p' + db_root_password + ' ' + db_name + ' >> db.rows.sql',
]));

gulp.task('compile', shell.task([
  'gradle clean war',
]));

gulp.task('deploy', ['compile'], shell.task([
  'if [ -d "' + static_web_path + '" ]; then sudo rm -rf ' + nginx_root + '/' + static_web_name + '; sudo cp -R ' + static_web_path + ' ' + nginx_root + '/' + static_web_name +'; sudo find ' + nginx_root + '/' + static_web_name + ' -type d -exec sudo chmod +rx {} \\;; fi',
  'pn=`cat settings.gradle | grep rootProject.name | cut -d "\'" -f 2` && c=`cat .env` && t=`cat templates/nginx.template` && echo "PROJECT_NAME=$pn\n$c\ncat<<EOF\n$t\nEOF" | bash > $pn.conf',
  'pn=`cat settings.gradle | grep rootProject.name | cut -d "\'" -f 2` && c=`cat .env` && t=`cat templates/nginx.https.template` && echo "PROJECT_NAME=$pn\n$c\ncat<<EOF\n$t\nEOF" | bash > $pn.conf.https',
  'if [ "Linux" = $(uname) ]; then sudo cp *.conf* ' + nginx_server_conf_path + '; else sudo mkdir -p ' + nginx_server_conf_path_mac + '; sudo cp *.conf* ' + nginx_server_conf_path_mac + '; fi',
  'if [ "Linux" = $(uname) ]; then ' + nginx_reload_cmd + '; else echo "Please make sure NGINX \\`include default.d/*.conf;\\` in server part, and work for port 80!"; ' + nginx_reload_cmd_mac + '; fi',
  'docker stop ' + docker_web_name,
  'sudo mv build/libs/*.war ' + docker_web_path,
  'docker start ' + docker_web_name
]));

